﻿using Google.Protobuf;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace GameServer
{
    public class ClientData
    {
        public class Data
        {
            public int m_FrameID;
            public Info m_Info;
        }

        public int m_ClientID
        {
            get;
            private set;
        }

        public bool m_LoginOver;

        //受到的数据
        ConcurrentQueue<Data> m_ConcurDictionary = new ConcurrentQueue<Data>();
        //开始进行模拟的数据
        ConcurrentQueue<Data> m_ConcurSimulationDictionary = new ConcurrentQueue<Data>();
        //目标最终所在地
        public XY m_xy
        {
            get;
            private set;
        }
        public ClientData(int id)
        {
            m_xy = new XY();
            m_ClientID = id;
        }

        public void Push(int frameID, byte[] bytes)
        {
            Info info = Info.Parser.ParseFrom(bytes);
            Data data = new Data();
            data.m_FrameID = frameID;
            info.Frame = frameID;
            data.m_Info = info;
            m_ConcurDictionary.Enqueue(data);
        }

        //将数据组装,将本帧之前的包括本帧的数据全部组装
        public byte[] DataAssemble()
        {
            ByteBuffer bt = new ByteBuffer();
            Data data;
            while (m_ConcurSimulationDictionary.TryDequeue(out data))
            {
                byte[] a = data.m_Info.ToByteArray();
                bt.WriteBytes(a);
            }
            return bt.mGetbuffer();
        }

        //一帧只能模拟一次输入操作
        public void Simulate(int frame)
        {
            Data data;
            if (m_ConcurDictionary.TryDequeue(out data))
            {
                data.m_Info.Frame = frame;
                m_xy.X += data.m_Info.Dir.X;
                m_xy.Y +=data.m_Info.Dir.Y;
                data.m_Info.Result = m_xy.Clone();
                data.m_Info.Isexecute = true;
                m_ConcurSimulationDictionary.Enqueue(data);
            }
        }
    }
}
